<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {


	/*
	 *登陆
	 */
	public function login()
	{
		$this->load->view('login.html');
	}

	public function loging()
	{
		$userdata = array();
		$userdata['user'] = $this->input->get_post('user');
		$userdata['password'] = $this->input->get_post('password');
		
		$vcode = $this->input->get_post('vcode');
		$vcode = strtoupper($vcode);
		
		$this->load->model('admin_model');
		$this->load->library('session');
		$this->session->set_userdata($userdata);

		header("Content-type: text/html; charset=utf-8");


		if( !empty($userdata['user']) && !empty($userdata['password']) ){
			$userdata['password'] = md5($userdata['password']);
			$rows = $this->admin_model->check($userdata);
			$userdata['authority'] = $rows['authority'];
			$userdata['id'] = $rows['id'];
			if($rows){
				if($vcode != NULL){
					if($vcode != $this->session->userdata('vcode')){
						echo "<script language=javascript>alert('验证码错误');history.back();</script>";
						exit;
					}
				}
				else{
					echo "<script language=javascript>alert('请输入验证码');history.back();</script>";
				}
				$this->session->set_userdata($userdata);
				echo "<script>location.href='http://dev.xlgvip.com/admin/cms';</script>";
				exit;
			}
			echo "<script language=javascript>alert('用户名密码错误');history.back();</script>";
		}
		else{
			echo "<script language=javascript>alert('用户名密码不能为空');history.back();</script>";
		}

	}

	
	/*
	 *加载后台
	 */
	public function cms()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		$this->load->model('columns_model');
		$data['column'] = $this->columns_model->get_column(0);
		$this->load->view('cms.html', $data);
	}


	/*
	 *加载栏目管理
	 */
	public function columns()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		$this->load->model('columns_model');
		$column1 = $this->columns_model->get_column(0);
		$data = array();
		foreach($column1 as $item){
			$id = $item['id'];
			$data['column1'][$id] = $item;
			$data['column1'][$id]['column2'] = $this->columns_model->get_column($id);
		}
		$this->load->view('content/columns.html', $data);
	}

	/*
	 *增加一级栏目
	 */
	public function add_column()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$column = $this->input->get_post('column');
		$model = $this->input->get_post('model');
		if(!isset($model))
			$model = '';
		$p_id = $this->input->get_post('p_id');
		if(!isset($p_id))
			$p_id = 0;
		$this->load->model('columns_model');
		$res = $this->columns_model->insert($column, $p_id, $model);
		echo $res;
	}

	/*
	 *修改栏目名称
	 */
	public function modcolumn()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$id = $this->input->get_post("id");
		$column_tile = $this->input->get_post("column_tile");
		$this->load->model('columns_model');
		$this->columns_model->update($id, $column_tile);
	}

	/*
	 *删除栏目
	 */
	public function delcolumn()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$id = $this->input->get_post("id");
		$this->load->model('columns_model');
		$this->columns_model->dele($id);
	}

	/*
	 *首页
	 */
	public function shouye()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$this->load->model('columns_model');
		$this->load->model('news_model');
		$id = $this->input->get_post('id');
		$column = $this->columns_model->get_column($id);
		$newthings = $this->news_model->get_new($column[0]['id']);
		$show = $this->news_model->get_new($column[1]['id']);
		$enjoy = $this->news_model->get_new($column[2]['id']);
		$data = array();
		$data['column'] = $column;
		$data['newthings'] = $newthings;
		$data['show'] = $show;
		$data['enjoy'] = $enjoy;
		$this->load->view('content/news/shouye.html', $data);
	}

	/*
	 *优惠
	 */
	public function youhui()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		$this->load->model('columns_model');
		$this->load->model('news_model');
		$id = $this->input->get_post('id');
		$column = $this->columns_model->get_column($id);
		$newthings = $this->news_model->get_new($column[0]['id']);
		$show = $this->news_model->get_new($column[1]['id']);
		$enjoy = $this->news_model->get_new($column[2]['id']);
		$data = array();
		$data['column'] = $column;
		$data['newthings'] = $newthings;
		$data['show'] = $show;
		$data['enjoy'] = $enjoy;
		$this->load->view('content/news/youhui.html', $data);
	}

	/*
	 *服务
	 */
	public function fuwu()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$this->load->model('columns_model');
		$this->load->model('news_model');
		$id = $this->input->get_post('id');
		$column = $this->columns_model->get_column($id);
		$newthings = $this->news_model->get_new($column[0]['id']);
		$show = $this->news_model->get_new($column[1]['id']);
		$enjoy = $this->news_model->get_new($column[2]['id']);
		$data = array();
		$data['column'] = $column;
		$data['newthings'] = $newthings;
		$data['show'] = $show;
		$data['enjoy'] = $enjoy;
		$this->load->view('content/news/fuwu.html', $data);
	}

	/*
	 *活动
	 */
	public function huodong()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$this->load->model('columns_model');
		$this->load->model('news_model');
		$id = $this->input->get_post('id');
		$column = $this->columns_model->get_column($id);
		$newthings = $this->news_model->get_new($column[0]['id']);
		$show = $this->news_model->get_new($column[1]['id']);
		$enjoy = $this->news_model->get_new($column[2]['id']);
		$data = array();
		$data['column'] = $column;
		$data['newthings'] = $newthings;
		$data['show'] = $show;
		$data['enjoy'] = $enjoy;
		$this->load->view('content/news/huodong.html', $data);
	}

	/*
	 *社团
	 */
	public function shetuan()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		$this->load->model('columns_model');
		$this->load->model('news_model');
		$id = $this->input->get_post('id');
		$column = $this->columns_model->get_column($id);
		$newthings = $this->news_model->get_new($column[0]['id']);
		$show = $this->news_model->get_new($column[1]['id']);
		$enjoy = $this->news_model->get_new($column[2]['id']);
		$data = array();
		$data['column'] = $column;
		$data['newthings'] = $newthings;
		$data['show'] = $show;
		$data['enjoy'] = $enjoy;
		$this->load->view('content/news/shetuan.html', $data);
	}

	/*
	 *vip卡
	 */
	public function vip()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$this->load->model('columns_model');
		$this->load->model('news_model');
		$id = $this->input->get_post('id');
		$column = $this->columns_model->get_column($id);
		$newthings = $this->news_model->get_new($column[0]['id']);
		$show = $this->news_model->get_new($column[1]['id']);
		$enjoy = $this->news_model->get_new($column[2]['id']);
		$data = array();
		$data['column'] = $column;
		$data['newthings'] = $newthings;
		$data['show'] = $show;
		$data['enjoy'] = $enjoy;
		$this->load->view('content/news/vip.html', $data);
	}

	/**
	 * 文件上传的处理
	 */
	public function upload()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		$data = array();
        $this->load->helper('zoom_image_helper');
        if ( !empty($_FILES) )
        {
            $this->load->helper('upload');
            //fileToUpload为表单name,返回http格式路径
            $path = upload('fileToUpload');
			
			$id = $this->input->get_post('p_id');
			$type = $this->input->get_post('type');
			if( $path ) {
                $newDir = date('Y/md', time());
                $dir = WEB_ROOT.'/upload/'.$newDir;
                //去掉http://头部分
                $sUrl = substr($path,7);
                $aUrl = explode("/",$sUrl);
                if(count($aUrl) > 1) {
                    unset($aUrl[0]);
                }
                $sUrl = implode("/",$aUrl);
                $imagePath = WEB_ROOT."/".$sUrl;
                $a = filesize($imagePath);
                if($a/1048576 > 2) {
					unlink($imagePath);
                    //echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
					header( 'Content-Type:text/html;charset=utf-8 ');
                    echo '<script>';
                    echo "alert('上传图片超过2M');";
                    echo 'history.back();';
                    echo '</script>';
                    exit;
                }
				$width = $this->input->get_post('width');
				$height = $this->input->get_post('height');

                $new_imagepath = imageSizeFormate($imagePath,$width,$height,$dir);
                $li = strlen(WEB_ROOT);
                $new_imagepath['new'] = SERVER_NAME.substr($new_imagepath['new'],$li);
                $new_imagepath['preview'] = SERVER_NAME.substr($new_imagepath['preview'],$li);

				$this->load->model('image_model');
				$res = $this->image_model->insert($new_imagepath['new'], $new_imagepath['preview'], $id, $type);

            } else {
                //echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
				header( 'Content-Type:text/html;charset=utf-8 ');
                echo '<script>';
                echo "alert('图片上传失败！');";
                echo 'history.back();';
                echo '</script>';
                exit;
            }
        } else {
            //echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
			header( 'Content-Type:text/html;charset=utf-8 ');
            echo '<script>';
            echo "alert('图片不能为空！');";
            echo 'history.back();';
            echo '</script>';
            exit;
        }
		header( 'Content-Type:text/html;charset=utf-8 ');
        echo '<script>';
        echo "alert('上传成功！');";
        echo 'history.back();';
        echo '</script>';
	}

	/*
	 *添加文章
	 */
	public function addnews()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

	
		$id = $this->input->get_post('id');
		$data['title'] = $this->input->get_post('title');
		$data['type'] = $this->input->get_post('type');
		$data['author'] = $this->input->get_post('author');
		$data['datetime'] = $this->input->get_post('datetime');
		$data['image'] = $this->input->get_post('image');
		$data['content'] = $this->input->get_post('content');
		$data['p_id'] = $this->input->get_post('p_id');
		
		
		$this->load->model('news_model');
		if($id == null){
			$res = $this->news_model->insert($data);
			echo $res;
			exit;
		}
		else{
			$res = $this->news_model->update($data, $id);
			echo $res;
		}
	}

	/*
	 *修改文章
	 */
	public function getupdatedata()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$id = $this->input->get_post('id');
		
		$this->load->model('news_model');
		$res = $this->news_model->get_upd_data($id);
		$data = array();
		$data['id'] = $res['id'];
		$data['json_data'] = json_decode($res['json_data'],true);
		header('Content-type: text/json');
		echo json_encode($data);
	}

	/*
	 *删除文章
	 */
	public function delnews()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$id = $this->input->get_post('id');
		
		$this->load->model('news_model');
		$res = $this->news_model->deletenews($id);
		echo $res;
	}

	/*
	 *选择新闻的配图
	 */
	public function getimg()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$p_id = $this->input->get_post('p_id');
		$this->load->model('image_model');
		$res = $this->image_model->getbypid($p_id);
		$data = array();
		$data['total'] = count($res);
		$data['json'] = $res;
		header('Content-type: text/json');
		echo json_encode($data);
	}

	/*
	 *获取服务器时间
	 */
	public function getdatetime()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		echo date("Y-m-d H:i:s");
	}

	/*
	 *文件管理
	 */
	public function filemanage()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$this->load->model("image_model");
		$img = $this->image_model->get_item();
		$i = 0;
		foreach($img as $item){
			$address = substr($item['img'], (strlen(SERVER_NAME)+1));
			$dir = explode('/', $address);
			$img[$i]['dir'] = array(
				'year' => $dir[1],
				'date' => $dir[2],
			);
			$i++;
		}
		
		$dir = array();
		$year = array();
		$date = array();

		foreach($img as $li){
			if(in_array($li['dir']['year'], $year)){
				if(in_array($li['dir']['date'], $date)){
					if(isset($dir[($li['dir']['year'])][($li['dir']['date'])]) && is_array($dir[($li['dir']['year'])][($li['dir']['date'])]))
						array_push($dir[($li['dir']['year'])][($li['dir']['date'])], $li);
					else{
						$dir[($li['dir']['year'])][($li['dir']['date'])] = array();
						array_push($dir[($li['dir']['year'])][($li['dir']['date'])], $li);
					}
				}
				else{
					array_push($date, $li['dir']['date']);
					if(isset($dir[($li['dir']['year'])][($li['dir']['date'])]) && is_array($dir[($li['dir']['year'])][($li['dir']['date'])]))
						array_push($dir[($li['dir']['year'])][($li['dir']['date'])], $li);
					else{
						$dir[($li['dir']['year'])][($li['dir']['date'])] = array();
						array_push($dir[($li['dir']['year'])][($li['dir']['date'])], $li);
					}
				}
			}
			else{
				array_push($year, $li['dir']['year']);
				array_push($date, $li['dir']['date']);
					if(isset($dir[($li['dir']['year'])][($li['dir']['date'])]) && is_array($dir[($li['dir']['year'])][($li['dir']['date'])]))
						array_push($dir[($li['dir']['year'])][($li['dir']['date'])], $li);
					else{
						$dir[($li['dir']['year'])][($li['dir']['date'])] = array();
						array_push($dir[($li['dir']['year'])][($li['dir']['date'])], $li);
					}
			}
		}
		$data['dir'] = $dir;
		$this->load->view("content/file.html", $data);
	}

	/*
	 *删除目录
	 */
	function deldir($dir)
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}

		$dh = opendir($dir);
		while ($file = readdir($dh)){
			if ($file != "." && $file != "..")
			{
				$fullpath = $dir . "/" . $file;
				if(!is_dir($fullpath)){
					unlink($fullpath);
				} 
				else{
					deldir($fullpath);
				}
			}
		}
		closedir($dh);
		if (rmdir($dir)){
			return true;
		}
		else{
			return false;
		}
	}

	/*
	 *删除指定日期的文件夹
	 */
	public function delImgByDate()
	{
		//安全验证
		$state = $this->identify();
		if($state != 1){
			echo "<script language=javascript>alert('请先登录！');window.location='http://dev.xlgvip.com/admin/login';</script>";
			exit;
		}


		$date = $this->input->get_post("date");
		$this->load->model("image_model");
		$imglist = $this->image_model->get_item();
		foreach($imglist as $item){
			$imgdate = substr($item['datetime'] , 0 , 10);
			$imgdate = explode("-", $imgdate);
			$imgdate = $imgdate[0].$imgdate[1].$imgdate[2];
			if($imgdate == $date){
				$res = $this->image_model->del($item['id']);
			}
		}
		$dir = WEB_ROOT."/upload/".substr($date, 0, 4)."/".substr($date,-4);
		$this->deldir($dir);
		echo $res;
	}

	/*
	 *安全验证
	 */
	public function identify(){
		$this->load->library('session');
		$data['user'] = $this->session->userdata('user');
		$data['password'] = $this->session->userdata('password');

		$this->load->model('admin_model');
		$res = $this->admin_model->check($data);
		if(!empty($res))
			return 1;
		else
			return 0;
	}
}

